//Card.java
package edu.game.chinesepoker;

/**
 *@author:youtubezou
 *@version:2012-7-4
 */
class Card implements Comparable<Card>{
	//public enum Color {BLACK, RED, FLOWER, CUBE};
	private char ch;	/*char to display*/
	private int val;	/*the compare value*/
	//private Color color;
	public static final char KING = 'W';
	public static final char LKING = 'w';
	public static final char TEN = 'D';
	public static final int CARD_TYPE_NUM = 15;

	Card (char ch) {
		if (ch != 'W' && ch != 'w')
			ch = Character.toUpperCase(ch);
		this.ch = ch;
		val = chToVal(ch);
	}
	
	Card (int val) {
		this.val = val;
		ch = valToCh(val);
	}
	
	public int compareTo (Card card){
		return val - card.val;
	}
	
	public boolean equals (Card card){
		if (card == null)
			return false;
		return val == card.val;
	}
	
	public String toString() {
		return  ""+ch;
	}
	
	void setCh(char ch){
		this.ch=ch;
	}
	
	char getCh () {
		return ch;
	}
	
	void setVal (int val) {
		this.val = val;
	}
	
	int getVal (){
		return val;
	}
	
	/*convert ch to val for compare method*/
	static int chToVal(char ch) {
		if (ch >= '3' && ch <= '9')
			return ch - '3';
		if (ch == 'D' || ch == 'd') 
			return 7;
		if (ch == 'J' || ch == 'j')
			return 8;
		if (ch == 'Q' || ch == 'q')
			return 9;
		if (ch == 'K' || ch == 'k')
			return 10;
		if (ch == 'A' || ch == 'a')
			return 11;
		if (ch == '2')
			return 12;
		if (ch == 'w')
			return 13;
		if (ch == 'W')
			return 14;
	
		return -1;		//unexcepted charactor
	}

	static char valToCh (int val) {
		if (val <= 6)
			return (char)('3'+val);
		switch (val) {
		case 7: return 'D';
		case 8:	return 'J';
		case 9: return 'Q';
		case 10: return 'K';
		case 11: return 'A';
		case 12: return '2';
		case 13: return 'w';
		case 14: return 'W';
		default:
			return '?';
		}
	}
}
